package com.qf.application.service.impl;

import com.alibaba.fastjson.JSON;
import com.qf.application.commons.utils.JWTUtils;
import com.qf.application.commons.utils.ServletUtils;
import com.qf.application.commons.utils.UUIDUtils;
import com.qf.application.entity.LoginUser;
import com.qf.application.entity.User;
import com.qf.application.service.IAllotService;
import net.sf.jsqlparser.expression.operators.relational.OldOracleJoinBinaryExpression;
import org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.util.JAXBSource;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class UserTokenService {

    private long timeout = 60 * 30 * 1000;

    @Autowired
    private IAllotService allotService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private String loginKey = "login_key:";

    public String createToken(User user) {

        // 1.查询登录用户的权限
        Set<String> userUrl = allotService.getUserUrl(user.getId());

        // 2、登录的对象都要封装到LoginUser
        LoginUser loginUser = new LoginUser();
        loginUser.setId(user.getId());
        loginUser.setUsername(user.getUsername());
        String uuid = UUIDUtils.getUuid();
        loginUser.setUuid(uuid);
        loginUser.setCreateTime(System.currentTimeMillis()); // 登录时间就是当前时间
        loginUser.setExTime(System.currentTimeMillis() + timeout); // 登录的过期时间是30m
        loginUser.setUser(user);
        loginUser.setRoles(null);
        loginUser.setPermissions(userUrl); // 权限在我们系统中及时当前用户拥有的URL
        loginUser.setLoginIp(ServletUtils.getRemoteAddr());

        // 3.把loginUser保存到reids中
        stringRedisTemplate.opsForValue().set(loginKey + uuid, JSON.toJSONString(loginUser), timeout, TimeUnit.MILLISECONDS);

        // 4.把UUID和用户名封装到JWT的Token中，然后把token字符串返回
        Map<String, Object> map = new HashMap<>();
        map.put("uuid", uuid);
        map.put("usenrame", user.getUsername());
        map.put("userId", user.getId().toString());
        String jwtToken = JWTUtils.createToken(map);
        return jwtToken; // 最终返回的是JWT生成的Token字符串
    }

    public static void main(String[] args) {
        User user = new User();
        user.setId(10);
        user.setPassword("123");
        user.setSex(1);

        System.out.println(user.toString());
        System.out.println(JSON.toJSONString(user));
    }

    public LoginUser getLoginUser(String uuid) {
        String json = stringRedisTemplate.opsForValue().get(loginKey + uuid);
        if (!ObjectUtils.isEmpty(json)) {
            return JSON.parseObject(json, LoginUser.class);
        }
        return null;
    }
}
